
package inf.coach.mobile;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;

public class AddCommentText extends Activity {
	
    public static long userid = 0;
    public static long osmid= 0;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.textanno);
        setTitle("Funktion Seite, Text Kommentar hinzufügen");
    
       	Bundle bundle = this.getIntent().getExtras();      
		userid = bundle.getLong("userid");
		osmid= bundle.getLong("osmid");
		
       final EditText comment = (EditText) findViewById(R.id.editText_comment);
       final CheckBox anonym= (CheckBox) findViewById(R.id.checkBox_textanno_anonym);
       final CheckBox self= (CheckBox) findViewById(R.id.checkBox_textanno_self);
       Button send=(Button)findViewById(R.id.button_send);
       
       send.setOnClickListener(new Button.OnClickListener(){
       	
       	public void onClick(View arg0){
       		
       		if(ConnectionReceiver.isInternetConnected(getApplicationContext()))
			{
								
			String text = webservice.createComment(userid, osmid, comment.getText().toString(),anonym.isChecked(),self.isChecked() );
			System.out.println(text);
			if (!text.equals(null) && text.equals("successful")) {
				dialog();

			}
			if (text.equals(null) || text.equals("failed")) {
				message.show(AddCommentText.this,"Fehlgeschlagen", "Bitte überprüfen Internetverbindung!");
			}
			}
   
       	}
       	
       });
       
        
    }
    
    protected void dialog() { 
        AlertDialog.Builder builder = new Builder(AddCommentText.this); 
        builder.setMessage("Automatisch zur letzten Seite zurück!"); 
        builder.setTitle("Erfolg"); 
        builder.setPositiveButton("ok", 
                new android.content.DialogInterface.OnClickListener() { 
                    @Override 
                    public void onClick(DialogInterface dialog, int which) { 
                        dialog.dismiss(); 
                        AddCommentText.this.finish();
                    }

                }); 
        
        builder.create().show(); 
    } 
    
    protected void dialoggoback() { 
        AlertDialog.Builder builder = new Builder(AddCommentText.this); 
        builder.setMessage("Möchten Sie zur letzten Seite zurück springen?"); 
        builder.setTitle("Information"); 
        builder.setPositiveButton("ok", 
                new android.content.DialogInterface.OnClickListener() { 
                    @Override 
                    public void onClick(DialogInterface dialog, int which) { 
                        dialog.dismiss(); 
                        AddCommentText.this.finish(); 
                    }

                }); 
        builder.setNegativeButton("abbrechen", 
                new android.content.DialogInterface.OnClickListener() { 
                    @Override 
                    public void onClick(DialogInterface dialog, int which) { 
                        dialog.dismiss(); 
                    } 
                }); 
        builder.create().show(); 
    } 
    @Override 
    public boolean onKeyDown(int keyCode, KeyEvent event) { 
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) { 
            dialoggoback(); 
            return false; 
        } 
        return false; 
    } 
}